package com.andon.microcellsimulator.http;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;

import com.andon.microcellsimulator.BuildConfig;
import com.andon.microcellsimulator.common.base.ZnkApplication;

public class Client {

    /*根据通讯协议设定的常量*/

    /**
     * 空的时间戳
     */
    public static final long EMPTY_TIME = 0L;

    private static final String OS = "android";

    private static final String APP = "IntelliHouse";

    public interface NetworkUnavailableListener {
        void networkUnavailable();
    }

//    /**
//     * 1.开发者账号登录
//     *
//     * @param phone    手机号
//     * @param password 密码
//     */
//    public static RequestHandle requestApiDeveloperLogin(Context context, String phone, String password, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
//        ApiDeveloperLoginRequest helper = new ApiDeveloperLoginRequest();
//        helper.setPhone(phone);
//        helper.setPassword(password);
//        return request(context, helper, ApiDeveloperLoginRequest.PATH, responseHandler, listener);
//    }

//    private static RequestHandle request(Context context, BaseRequest helper, String path, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
//
//        if (context == null) {
//            Log.w("HTTP Request " + path, "Context == null");
//            return null;
//        }
//
//        if (!isNetworkConnect(context)) {
//            if (listener != null) {
//                listener.networkUnavailable();
//            }
//            return null;
//        }
//
//        String appID = ((ZnkApplication) context.getApplicationContext()).getAppID();
//
//        if (TextUtils.isEmpty(appID)) {
//            appID = Installation.id(context);
//        } else {
//            appID += BuildConfig.FLAVOR;
//        }
//
//        HeadInfo.Builder builder = new HeadInfo.Builder(context).os(OS).app(APP).appID(appID);
//        helper.setHeadInfo(builder.build());
//        InnovationClient client = InnovationClient.getInstance();
//        client.setSSLSocketFactory();
//        Log.w("HTTP Request " + path, helper.getRequestParams().toString());
//        return client.post(context, helper.getPathWithHeadInfo(path), helper.getRequestParams(), responseHandler);
//    }

    public static boolean isNetworkConnect(Context context) {
        ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo mobileInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mobileInfo != null) {
            NetworkInfo.State mobileState = mobileInfo.getState();
            if (mobileState != null && (mobileState == NetworkInfo.State.CONNECTED || mobileState == NetworkInfo.State.CONNECTING)) {
                return true;
            }
        }

        if (wifiInfo != null) {
            NetworkInfo.State wifiState = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            if (wifiState != null && (wifiState == NetworkInfo.State.CONNECTED || wifiState == NetworkInfo.State.CONNECTING)) {
                return true;
            }
        }

        return false;
    }
}
